#encoding:UTF-8
from tornado import ioloop, web
import tornado
import tornado.options
import datetime
import time
from datetime import date
from time import mktime
import json
import config
from lib import mydb

c = mydb.WriteItem()
TIME_CONFIG_LOG = {
                   1:19,
                   2:17,
                   3:16
                   }
class Xoso(web.RequestHandler):
    
    def gettransaction(self):
        myDict = {}
        writeDict = {}
        try:
            num = self.get_argument('num')
            get = self.get_argument('get')
            getTransaction = "CALL get_all_transaction('%s','%s')"%(num,get)
            allTran = c.excuteReturn(getTransaction)
            allArrStr = []
            for tran in allTran:
                user = str(tran['id_user'])
                nameType = str(tran['name_type'])
                ngayChoi = str(tran['ngay'])
                tinh = str(tran['tentinh'])
                playCard = json.dumps(tran['play_card'])
                myDict['tinh']= tinh
                myDict['card'] = playCard
                myDict['ngay'] = ngayChoi
                myDict['loai'] = nameType
                myDict['player'] = user
                allArrStr.append(myDict)
            writeDict['title'] = 'ok'
            writeDict['data'] = allArrStr
            self.write(writeDict)
            self.finish()
        except:
            result = {}
            result['error_code'] = 1
            result['msg'] = 'Loi Khac'
            self.write(json.dumps(result))
            self.finish()
    def getmytransaction(self):
        myDict = {}
        writeDict = {}
        try:
            user = self.get_argument('id_user')
            num = self.get_argument('num')
            get = self.get_argument('get')
            getTransaction = "CALL get_my_transaction('%s','%s','%s')"%(user,num,get)
            myTran = c.excuteReturn(getTransaction)
            myArrStr = []
            for tran in myTran:
                nameType = str(tran['name_type'])
                ngayChoi = str(tran['ngay'])
                tinh = str(tran['tentinh'])
                playCard = json.dumps(tran['play_card'])
                myDict['tinh']= tinh
                myDict['card'] = playCard
                myDict['ngay'] = ngayChoi
                myDict['loai'] = nameType
                myArrStr.append(myDict)
                print tinh
            writeDict['title'] = "ok"
            writeDict['data'] = myArrStr
            self.write(writeDict)
            self.finish()
        except:
            result = {}
            result['error_code'] = 1
            result['msg'] = 'Loi Khac'
            self.write(json.dumps(result))
            self.finish() 
    def playlode(self):
        myresult = {}
        try:
            user = self.get_argument('id_user')
            area = int(self.get_argument('id_area'))
            district = int(self.get_argument('id_district'))
            my_type = int(self.get_argument('id_type'))
            card = self.get_argument('play_card')
            mydate = date.today()
            current_date = int(mktime(mydate.timetuple()))
            queryInsert = "CALL insert_play_card_log('%s','%s','%s','%s','%s','%s')" % \
                    (user,area,district,my_type,card,current_date)
            print user
            if datetime.datetime.now().hour < TIME_CONFIG_LOG[area]:
                c.excuteNonReturn(queryInsert)
                myresult['code'] = 0
                myresult['msg'] = "Thành công!"
                self.write(json.dumps(myresult))
                print "Insert thanh cong vao nguoi choi lo de"
            else:
                myresult['error_code'] = 1
                myresult['msg'] = "Đã quá thời gian quy định để chơi. Bạn không được chơi!"
                self.write(json.dumps(myresult))
        except:
            result = {}
            result['error_code'] = 1
            result['msg'] = 'Loi Khac'
            self.write(json.dumps(result))
        
    def get(self, path):
        if path.startswith('playlode'):
            return self.playlode()
        elif path.startswith('getmytransaction'):
            return self.getmytransaction()
        elif path.startswith('gettransaction'):
            return self.gettransaction()
